package com.axend.aerosense.dev.adapter;

import android.view.View;

import com.axend.aerosense.common.adapter.BaseBindingAdapter;
import com.axend.aerosense.common.bean.RadarBean;
import com.axend.aerosense.common.util.StringLengthUtils;
import com.axend.aerosense.dev.R;
import com.axend.aerosense.dev.databinding.DevLayoutDeviceItemBinding;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @author ChenHongWei
 * @date :2021/1/5 16:36
 */

public class DevDeviceListAdapter
        extends BaseBindingAdapter<RadarBean, DevLayoutDeviceItemBinding> {

    private OnItemClickListener onItemClickListener;
    private OnItemLongClickListener onItemLongClickListener;
    private OnItemCountChangeListener onItemCountChangeListener;
    private boolean isEnableClick = true;  //是否启用点击、长按事件
    private boolean isShowCheckBox = false;  //是否显示checkbox
    private boolean isSelectAll = false;  //是否全选
    private Map<Integer, RadarBean> checkedItems = new HashMap<>();  //已选中项
    private int currentSelect = -1;

    public Map<Integer, RadarBean> getCheckedItems() {
        return checkedItems;
    }

    public DevDeviceListAdapter(List<RadarBean> items) {
        super(items);
    }

    @Override
    protected int getLayoutResId(int viewType) {
        return R.layout.dev_layout_device_item;
    }

    @Override
    protected void onBindItem(DevLayoutDeviceItemBinding binding, RadarBean item, int position) {
        int imageId = item.getStateImageId();
        if(item.getRadarState() == 1){
            // 离线展示提示
            binding.devOffTv.setVisibility(View.VISIBLE);
        }else{
            binding.devOffTv.setVisibility(View.GONE);
        }
        binding.devIvStatus.setImageResource(imageId);
        binding.devTvDeviceName.setText(StringLengthUtils.getSimpleString(item.getRadarName(), 15));
        binding.devTvLocation.setText(StringLengthUtils.getSimpleString(item.getRoomName(), 15));
        binding.devUpgradeable.setVisibility(item.getRadarVersionState() ? View.VISIBLE : View.GONE);
        binding.devIvCheck.setVisibility(isShowCheckBox ? View.VISIBLE : View.GONE);
        binding.devDeviceImg.setImageResource(item.getType().getImageRes());
        binding.devUnassignedTag.setVisibility(item.isUnassigned() ? View.VISIBLE : View.GONE);
        binding.devTvLocation.setVisibility(item.isUnassigned() ? View.GONE : View.VISIBLE);

        binding.devDeviceItemConstraintLayout.setBackground(
                item.isUnassigned() ?
                        binding.getRoot().getContext().getDrawable(R.drawable.common_unassigned_bg_selector) :
                        binding.getRoot().getContext().getDrawable(R.drawable.dev_fillet_item_selector));

        setIamgeChecked(binding, isSelectAll);
        if (currentSelect == position){

        }
        binding.devDeviceItemConstraintLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onItemClickListener != null){
                    if (isEnableClick){
                        onItemClickListener.onClick(item, position);
                    }else {
                        binding.devIvCheck.performClick();
                    }
                }
            }
        });
        binding.devDeviceItemConstraintLayout.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if (isEnableClick){
                    return onItemLongClickListener != null ?
                            onItemLongClickListener.onLongClick(item, position) : false;
                }
                return false;
            }
        });
        binding.devIvCheck.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                boolean tag = (boolean) v.getTag();
                if (tag){
                    setIamgeChecked(binding, false);
                    checkedItems.remove(position);
                }else {
                    setIamgeChecked(binding, true);
                    checkedItems.put(position, item);
                }
                if (onItemCountChangeListener != null){
                    onItemCountChangeListener.onChange(checkedItems);
                }
            }
        });
        /*binding.devCheckbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked){
                    checkedItems.put(position, item);
                }else {
                    checkedItems.remove(position);
                }

                if (onItemCountChangeListener != null){
                    onItemCountChangeListener.onChange(checkedItems);
                }
            }
        });*/
    }

    //是否选中
    private void setIamgeChecked(DevLayoutDeviceItemBinding binding, boolean b){
        if (b){
            binding.devIvCheck.setImageResource(R.drawable.common_select);
            binding.devIvCheck.setTag(true);
        }else {
            binding.devIvCheck.setImageResource(R.drawable.common_unselected);
            binding.devIvCheck.setTag(false);
        }
    }

    /**
     * 设置checkbox是否显示
     */
    public void setCheckBoxVisibility(boolean isShow){
        isShowCheckBox = isShow;
        notifyDataSetChanged();
    }

    /**
     * 设置是否全选
     * @param selectAll
     */
    public void setSelectAll(boolean selectAll) {
        isSelectAll = selectAll;
        if (selectAll){
            for(int i = 0; i < items.size(); i++){
                checkedItems.put(i, items.get(i));
            }
        }else {
            checkedItems.clear();
        }
        if (onItemCountChangeListener != null){
            onItemCountChangeListener.onChange(checkedItems);
        }
        notifyDataSetChanged();
    }

    /**
     * 删除已选项
     */
    public void deleteItem(){
        List<RadarBean> tempList = new ArrayList<>();
        if (checkedItems.size() > 0){
            //把没有删除的提取出来
            for (int i = 0; i < items.size(); i++){
                if (checkedItems.get(i) == null){
                    tempList.add(items.get(i));
                }
            }
            items = tempList;
            notifyDataSetChanged();
            checkedItems.clear();
            if (onItemCountChangeListener != null){
                onItemCountChangeListener.onChange(checkedItems);
            }
        }
    }

    /**
     * 是否启动用 点击 事件或 长按 事件
     * @param enableClick
     */
    public void setEnableClick(boolean enableClick) {
        isEnableClick = enableClick;
    }

    /**
     * 设置监听
     * @param onItemClickListener
     */
    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    /**
     * 设置长按事件监听
     * @param onItemLongClickListener
     */
    public void setOnItemLongClickListener(OnItemLongClickListener onItemLongClickListener) {
        this.onItemLongClickListener = onItemLongClickListener;
    }

    /**
     * 设置item选中或取消选中改变回调
     * @param onItemCountChangeListener
     */
    public void setOnItemCountChangeListener(OnItemCountChangeListener onItemCountChangeListener) {
        this.onItemCountChangeListener = onItemCountChangeListener;
    }

    /**
     * 每个item点击事件
     */
    public interface OnItemClickListener{
        void onClick(RadarBean item, int position);
    }

    /**
     * 每个item长按事件
     */
    public interface OnItemLongClickListener{
        boolean onLongClick(RadarBean item, int position);
    }

    /**
     * item选中或取消选中改变回调
     */
    public interface OnItemCountChangeListener{
        void onChange(Map<Integer, RadarBean> checkedItems);
    }
}
